package se.penselnsvag.bookdb.module.user
{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import se.penselnsvag.bookdb.tech.rpc.DataListener;
	
	public class UserService
	{
		private static var instance:UserService;
		
		private var ro:RemoteObject;
		
		public static function getInstance():UserService {
			if(instance==null)
				instance= new UserService(new SingletonBlocker());
				
			return instance;
		}
		
		public function UserService(sb:SingletonBlocker)
		{
			if(sb==null)
				throw new Error("UserService is a singleton user UserService.getInstance() instead!");
			init();
		}
		
		private function init():void {
			ro = new RemoteObject("amfphp");
			ro.source="bookdb.UserService";
		}
		
		public function getAllUsers(dl:DataListener):void {
			addListeners(dl);
			ro.getAllUsers();
		}
		
		public function saveUser(dl:DataListener, u:User):void {
			addListeners(dl);
			ro.saveUser(u);				
		}
		
		private function addListeners(dl:DataListener):void {
			ro.addEventListener(ResultEvent.RESULT, dl.dataUpdate);
			ro.addEventListener(FaultEvent.FAULT, dl.dataFault);			
		}

	}
}

class SingletonBlocker {}